package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.entity.JsonResult;
import com.csun.cmny.provider.pojo.*;
import com.csun.cmny.provider.service.CacBaseTypeService;
import com.csun.cmny.provider.service.CacBigItemService;
import com.csun.cmny.provider.service.CacMinorItemService;
import com.csun.cmny.provider.service.CacTypeService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 评估类别管理——机构额外类别
 * */
@RestController
@RequestMapping("/CacType")
//@RequestMapping("/auth")
public class CacTypeController extends BaseController {
    @Autowired
    private CacTypeService cacTypeService;
    @Autowired
    private CacMinorItemService cacMinorItemService;
    @Autowired
    private CacBigItemService cacBigItemService;
    @Autowired
    private CacBaseTypeService cacBaseTypeService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * */
    //添加单个类别
    @PostMapping("/addCacType")
    public Wrapper<CacType> addCacType(@RequestParam("name") String name) {
        CacType cacType = new CacType();
        Integer homeid = 1001;
        cacType.setHomeId(homeid);
        cacType.setName(name);

        //根据类别名称判断该类别是否在该机构中已经存在
        CacType cacType1 = cacTypeService.selectByCacType(cacType);
        if (cacType1 != null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该类别已经存在",null);
        }

        if (cacType.getName() == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别名称不能为空",null);
        }
        int n = cacTypeService.insert(cacType);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"添加失败",cacType);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"添加成功",cacType);
    }

    //根据id删除单个类别及类别下的大项和大项下的小项
    @RequestMapping(value = "/delCacType", method = RequestMethod.POST)
    public Wrapper delCacType(Integer cacTypeId) {
        if (cacTypeId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        //获取当前用户的机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        CacType cacType = new CacType();
        cacType.setCacTypeId(cacTypeId);
        //查询该列表
        CacType cacType1 = cacTypeService.selectByCacType(cacType);
        if (cacType1 == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",cacTypeId);
        }
        if (!cacType1.getHomeId().equals(homeId)) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"您不是当前机构人员,无法删除",cacTypeId);
        }
        //删除小项
        int m = cacMinorItemService.delByTypeId(cacTypeId);
        //删除大项
        int b = cacBigItemService.delByTypeId(cacTypeId);
        //删除类别
        int n = cacTypeService.delById(cacTypeId);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"删除失败",n);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"删除成功",n);
    }

    //修改单个类别
    @RequestMapping(value = "/updateCacType", method = RequestMethod.POST)
    public Wrapper<CacType> updateCacType(CacType cacType) {
        if (cacType.getCacTypeId() == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        if (cacType.getName() == null || cacType.getName().length() <= 0) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别名称不能为空",null);
        }

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        CacType cacType1 = new CacType();
        cacType1.setHomeId(homeId);
        cacType1.setName(cacType.getName());

        //通过名称和机构id查询，判断该类别名称在该机构中是否已经存在
        CacType cacType2 = cacTypeService.selectByCacType(cacType1);
        if (cacType2 != null && !cacType2.getCacTypeId().equals(cacType.getCacTypeId())) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该名称已经存在",cacType);
        }

        int n = cacTypeService.update(cacType);
        if (n <= 0) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"修改失败",cacType);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"修改成功",cacType);
    }

    //根据类别id查询单个类别
    @RequestMapping(value = "/selectCacTypeById", method = RequestMethod.POST)
    public Wrapper<CacType> selectCacTypeById(Integer cacTypeId) {
        if (cacTypeId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        CacType cacType = cacTypeService.selectCacTypeById(cacTypeId);
        if (cacType == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",null);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",cacType);
    }

    //根据类别id查询单个类别及类别下的大项及大项下的小项
    @RequestMapping(value = "/selectOneById", method = RequestMethod.POST)
    public Wrapper selectOneById(Integer cacTypeId) {
        if (cacTypeId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"类别id不能为空",null);
        }
        CacType cacType = cacTypeService.selectOneById(cacTypeId);
        if (cacType == null) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"没有此id",null);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",cacType);
    }

    //查询社区的所有类别及类别下的大项及大项下的小项
    @RequestMapping(value = "/selectShowAll", method = RequestMethod.POST)
    public Wrapper selectShowAll() {
        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        List<CacType> cacTypeList = cacTypeService.selectShowAll(homeId);
        if (CollectionUtils.isEmpty(cacTypeList)) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"该机构还未初始化评估",homeId);
        }
        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",cacTypeList);
    }

    //将基础的评估初始化到社区
    @RequestMapping(value = "/initCac", method = RequestMethod.POST)
    public Wrapper initCax(Integer homeId) {
        if (homeId == null) {
            return  WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_,"机构id不能为空",null);
        }
        //判断该机构是否存在

        //查询基础表的所有类别、大项、小项
        List<CacBaseType> cacBaseTypeList = cacBaseTypeService.selectShowAll(getLoginAuthDto().getGroupId());
        if (CollectionUtils.isEmpty(cacBaseTypeList)) {
            return  WrapMapper.wrap(Wrapper.ERROR_CODE,"基础评估信息还未初始化",null);
        }
        //查询该社区的所有类别、大项、小项
        List<CacType> cacTypeList = cacTypeService.selectShowAll(homeId);

        //情况1：当社区集合为空，将基础的所有初始化到社区
        if (CollectionUtils.isEmpty(cacTypeList)) {
            for (int i = 0; i < cacBaseTypeList.size(); i++) {
                //基础类别对象
                CacBaseType cacBaseType = cacBaseTypeList.get(i);
                //获取类别信息，社区类别对象
                CacType cacType = new CacType();
                cacType.setName(cacBaseType.getName());
                cacType.setHomeId(homeId);
                //插入类别
                int n = cacTypeService.insert(cacType);
                //如果插入失败就跳出此次循环，如果不跳出，在插入大项时得不到类别id
                if (n <= 0) {
                    continue;
                }
                //基础大项集合
                List<CacBaseBigItem> cacBaseBigItemList = cacBaseType.getCacBaseBigItemList();
                for (int j = 0; j < cacBaseBigItemList.size(); j++) {
                    //基础大项对象
                    CacBaseBigItem cacBaseBigItem = cacBaseBigItemList.get(j);
                    //获取社区大项信息，社区大项大项
                    CacBigItem cacBigItem = new CacBigItem();
                    cacBigItem.setCacTypeId(cacType.getCacTypeId());
                    cacBigItem.setType(cacBaseBigItem.getType());
                    cacBigItem.setInstruction(cacBaseBigItem.getInstruction());
                    cacBigItem.setName(cacBaseBigItem.getName());
                    //插入大类
                    int b = cacBigItemService.insert(cacBigItem);
                    //如果插入失败就跳出此次循环，如果不跳出，在插入小项时得不到大项id
                    if (b <= 0) {
                        continue;
                    }
                    //基础小项集合
                    List<CacBaseMinorItem> cacBaseMinorItemList = cacBaseBigItem.getCacBaseMinorItemList();
                    for (int k = 0; k < cacBaseMinorItemList.size(); k++) {
                        //基础信息对象
                        CacBaseMinorItem cacBaseMinorItem = cacBaseMinorItemList.get(k);
                        //获取小项信息，社区小项对象
                        CacMinorItem cacMinorItem = new CacMinorItem();
                        cacMinorItem.setBigItemId(cacBigItem.getBigItemId());
                        cacMinorItem.setName(cacBaseMinorItem.getName());
                        cacMinorItem.setInstruction(cacBaseMinorItem.getInstruction());
                        cacMinorItem.setGrade(cacBaseMinorItem.getGrade());
                        //插入小项
                        int m = cacMinorItemService.insert(cacMinorItem);
                    }
                }
            }
        } else {//情况2：当当前社区的集合不为空
            for (int i = 0; i < cacBaseTypeList.size(); i++) {
                CacBaseType cacBaseType = cacBaseTypeList.get(i);
                for (int j = 0; j < cacTypeList.size(); j++) {
                    CacType cacType = cacTypeList.get(j);
//                    System.out.println("cacBaseType:" + cacBaseType.toString());
//                    System.out.println("cacType:" + cacType.toString());
//                    Boolean a = false;
//                    if (cacBaseType.getName().equals(cacType.getName())) {
//                        a = true;
//                    }
//                    System.out.println("a:" + a);

                    Boolean flaga = true;//类别名称匹配标识：没有匹配到为true-意味着需要插入；匹配到了为false-意味着不需要插入
                    //判断类别名称是否相同——类别名称匹配
                    if (cacBaseType.getName().equals(cacType.getName())) {//如果相同
                        flaga = false;
                        //基础的大项集合
                        List<CacBaseBigItem> cacBaseBigItemList = cacBaseType.getCacBaseBigItemList();
                        //社区的大项集合
                        List<CacBigItem> cacBigItems = cacType.getCacBigItemList();
                        for (int k = 0; k < cacBaseBigItemList.size(); k++) {
                            CacBaseBigItem cacBaseBigItem = cacBaseBigItemList.get(k);
                            for (int l = 0; l < cacBigItems.size(); l++) {
                                CacBigItem cacBigItem = cacBigItems.get(l);
//                                System.out.println("cacBaseBigItem:" + cacBaseBigItem.toString());
//                                System.out.println("cacBigItem:" + cacBigItem.toString());
//                                Boolean b = false;
//                                if (cacBaseBigItem.getName().equals(cacBigItem.getName())) {
//                                    b = true;
//                                }
//                                System.out.println("b:" + b);

                                Boolean flagb = true;//大项名称匹配标识：没有匹配到为true-意味着需要插入；匹配到了为false-意味着不需要插入
                                //判断大项名称是否相同
                                if (cacBaseBigItem.getName().equals(cacBigItem.getName())) {//如果相同
                                    flagb = false;
                                    //基础的小项集合
                                    List<CacBaseMinorItem> cacBaseMinorItems = cacBaseBigItem.getCacBaseMinorItemList();
                                    //社区的小项集合
                                    List<CacMinorItem> cacMinorItems = cacBigItem.getCacMinorItemList();
                                    for (int m = 0; m < cacBaseMinorItems.size(); m++) {
                                        CacBaseMinorItem cacBaseMinorItem = cacBaseMinorItems.get(m);
                                        for (int n = 0; n < cacMinorItems.size(); n++) {
                                            CacMinorItem cacMinorItem = cacMinorItems.get(n);
//                                            System.out.println("cacBaseMinorItem:" + cacBaseMinorItem.toString());
//                                            System.out.println("cacMinorItem:" + cacMinorItem.toString());
//                                            Boolean c = false;
//                                            if (cacBaseMinorItem.getName().equals(cacMinorItem.getName())) {
//                                                c = true;
//                                            }
//                                            System.out.println("c:" + c);

                                            Boolean flagc = true;//小项名称匹配标识：没有匹配到为true-意味着需要插入；匹配到了为false-意味着不需要插入
                                            //判断小项名称是否相同
                                            if (cacBaseMinorItem.getName().equals(cacMinorItem.getName())) {//如果相同，就不需要插入
                                                flagc = false;
                                                //小项名称匹配到了，就退出社区小项循环，进入基础小项的下一次循环
                                                break;

                                            } else if (n == cacMinorItems.size() - 1 && flagc) {//如果匹配到最后一条都没有匹配到就需要插入该小项
                                                //情况2.3：当大项名称相同，但是小项名称不同
                                                //获取小项信息
                                                CacMinorItem cacMinorItem1 = new CacMinorItem();
                                                cacMinorItem1.setBigItemId(cacBigItem.getBigItemId());
                                                cacMinorItem1.setName(cacBaseMinorItem.getName());
                                                cacMinorItem1.setInstruction(cacBaseMinorItem.getInstruction());
                                                cacMinorItem1.setGrade(cacBaseMinorItem.getGrade());
                                                //插入小项
                                                int o = cacMinorItemService.insert(cacMinorItem1);
//                                                System.out.println("类别名称和大项名称相同，小项不同，基础类别名称：" + cacBaseType.getName() + " ," + "社区类别名称：" + cacType.getName());
//                                                System.out.println("基础大项名称：" + cacBaseBigItem.getName() + " ," + "社区大项名称：" + cacBigItem.getName());
//                                                System.out.println("基础小项名称：" + cacMinorItem1.getName() + " ," + "社区小项名称：" + cacMinorItem.getName());
                                            }
                                        }
                                    }
                                    //大项名称匹配到了，就退出社区大项循环，进入基础大项的下一次循环
                                    break;
                                } else if (l == cacBigItems.size() - 1 && flagb) {//如果匹配到最后一条都没有匹配到就需要插入该大项
                                    //情况2.2：当类别名称相同，但是大项名称不同
                                    //获取大项信息
                                    CacBigItem cacBigItem1 = new CacBigItem();
                                    cacBigItem1.setCacTypeId(cacType.getCacTypeId());
                                    cacBigItem1.setType(cacBaseBigItem.getType());
                                    cacBigItem1.setInstruction(cacBaseBigItem.getInstruction());
                                    cacBigItem1.setName(cacBaseBigItem.getName());
                                    //插入大项
                                    int c = cacBigItemService.insert(cacBigItem1);
//                                    System.out.println("类别名称相同，大项不同，基础类别名称：" + cacBaseType.getName() + " ," + "社区类别名称：" + cacType.getName());
//                                    System.out.println("基础大项名称：" + cacBaseBigItem.getName() + " ," + "社区大项名称：" + cacBigItem.getName());
                                    //如果插入失败就跳出此次循环，如果不跳出，在插入小项时得不到大项id
                                    if (c <= 0) {
                                        continue;
                                    }
                                    //小项集合
                                    List<CacBaseMinorItem> cacBaseMinorItemList = cacBaseBigItem.getCacBaseMinorItemList();
                                    for (int m = 0; m < cacBaseMinorItemList.size(); m++) {
                                        CacBaseMinorItem cacBaseMinorItem = cacBaseMinorItemList.get(m);
                                        //获取小项信息
                                        CacMinorItem cacMinorItem = new CacMinorItem();
                                        cacMinorItem.setBigItemId(cacBigItem1.getBigItemId());
                                        cacMinorItem.setName(cacBaseMinorItem.getName());
                                        cacMinorItem.setInstruction(cacBaseMinorItem.getInstruction());
                                        cacMinorItem.setGrade(cacBaseMinorItem.getGrade());
                                        //插入小项
                                        int n = cacMinorItemService.insert(cacMinorItem);
                                    }
                                }
                            }
                        }
                        //类别名称匹配到了，就退出社区类别循环，进入基础类别的下一次循环
                        break;
                    } else if (j == cacTypeList.size() - 1 && flaga) {//如果匹配到最后一条都没有匹配到就需要插入该类别
                        //情况2.1:当该类别在该社区中不存在时，需要插入该类别及类别下的大项及大项下的小项
                        //获取类别信息
                        CacType cacType1 = new CacType();
                        cacType1.setName(cacBaseType.getName());
                        cacType1.setHomeId(homeId);
                        //插入类别
                        int n = cacTypeService.insert(cacType1);
                        //如果插入失败就跳出此次循环，如果不跳出，在插入大项时得不到类别id
                        if (n <= 0) {
                            continue;
                        }
                        //大项集合
                        List<CacBaseBigItem> cacBaseBigItemList = cacBaseType.getCacBaseBigItemList();
                        for (int b = 0; b < cacBaseBigItemList.size(); b++) {
                            CacBaseBigItem cacBaseBigItem = cacBaseBigItemList.get(b);
                            //获取大项信息
                            CacBigItem cacBigItem = new CacBigItem();
                            cacBigItem.setCacTypeId(cacType1.getCacTypeId());
                            cacBigItem.setType(cacBaseBigItem.getType());
                            cacBigItem.setInstruction(cacBaseBigItem.getInstruction());
                            cacBigItem.setName(cacBaseBigItem.getName());
                            //插入大类
                            int d = cacBigItemService.insert(cacBigItem);
                            //如果插入失败就跳出此次循环，如果不跳出，在插入小项时得不到大项id
                            if (d <= 0) {
                                continue;
                            }
                            //小项集合
                            List<CacBaseMinorItem> cacBaseMinorItemList = cacBaseBigItem.getCacBaseMinorItemList();
                            for (int k = 0; k < cacBaseMinorItemList.size(); k++) {
                                CacBaseMinorItem cacBaseMinorItem = cacBaseMinorItemList.get(k);
                                //获取小项信息
                                CacMinorItem cacMinorItem = new CacMinorItem();
                                cacMinorItem.setBigItemId(cacBigItem.getBigItemId());
                                cacMinorItem.setName(cacBaseMinorItem.getName());
                                cacMinorItem.setInstruction(cacBaseMinorItem.getInstruction());
                                cacMinorItem.setGrade(cacBaseMinorItem.getGrade());
                                //插入小项
                                int m = cacMinorItemService.insert(cacMinorItem);
//                                System.out.println("类别不存在，基础类别名称：" + cacBaseType.getName() + " ," + "社区类别名称：" + cacType1.getName());
//                                System.out.println("基础大项名称：" + cacBaseBigItem.getName() + " ," + "社区大项名称：" + cacBigItem.getName());
//                                System.out.println("基础小项名称：" + cacBaseMinorItem.getName() + " ," + "社区小项名称：" + cacMinorItem.getName());
                            }
                        }
                    }
                }
            }
        }

        return  WrapMapper.wrap(Wrapper.SUCCESS_CODE,"查询成功",null);
    }
}
